#include "fldrv_board.h"
#include "flapp_display.h"
#include "flapp_system.h"
#include "flapp_peripheral.h"
#include "flapp_bat_manage.h"
#include "flapp_net_slm.h"
#include "flapp_clock.h"

typedef struct 
{			
	bool fLedOnOff;	//动作灯灭标志，只有在闪烁的灯光灭了才进入休眠
	bool fRefreshLed;	//发送标志	
	bool bMedLedSta;	//服药灯状态
	bool bSubVisitLedSta;	//随访灯状态
	bool bNetLedSta;	//随访灯状态
	u8 u8NetConnSuc;	//网络连接成功亮灯5秒
	u8 u8LedDisTmr;	//发送标志		
	u8 u8LedDisStep;	//发送标志		
	u8 u8AlarmCheck;	//报警检测
}flapp_led_para_t;	//通讯获取到的参数存储，有需要就获取

flapp_led_para_t led_para;

void flapp_alarm_set_check()
{
	led_para.u8AlarmCheck++;
	if(led_para.u8AlarmCheck > 200)
	{
		led_para.u8AlarmCheck = FALSE;
		fldrv_ertc_alarm_value_set(ALARM_TIME);
	}
}

/**
* @brief  解析设备数据
  * @param  void
  * @retval none
  */
void flapp_display_run(void)
{
	flapp_sys_para_t sys_led_para;
	sys_led_para = flapp_get_sys_para();
	
	if(sys_led_para.bTestMode)	return; 
	
	if(sys_led_para.bPowOnOff == OFF)
	{
		LED_NET_BAD_OFF;
		LED_SUB_VIS_OFF;
		LED_LOW_POW_OFF;
		LED_MED_ALA_OFF;
		LED_CHAR_FULL_OFF;
		LED_NET_OK_OFF;
		led_para.u8LedDisTmr = FALSE;
		led_para.u8LedDisStep = DIS_ALL_ON;
		return;
	}
	led_para.u8LedDisTmr++;
	
	flapp_alarm_set_check();
	
	if(led_para.u8NetConnSuc)
		led_para.u8NetConnSuc--;
	switch(led_para.u8LedDisStep)
	{
		case DIS_ALL_ON:
			LED_NET_BAD_ON;
			LED_SUB_VIS_ON;
			LED_LOW_POW_ON;
			LED_MED_ALA_ON;
			LED_CHAR_FULL_ON;
			LED_NET_OK_ON;
			if(led_para.u8LedDisTmr > POW_ON_LED_ON_TMR)
			{
				led_para.u8LedDisTmr = FALSE;
				led_para.u8LedDisStep = DIS_ALL_OFF;
			}
			break;
		case DIS_ALL_OFF:
			LED_NET_BAD_OFF;
			LED_SUB_VIS_OFF;
			LED_LOW_POW_OFF;
			LED_MED_ALA_OFF;
			LED_CHAR_FULL_OFF;
			LED_NET_OK_OFF;
			if(led_para.u8LedDisTmr > POW_ON_LED_ON_TMR + 20)
			{
				led_para.u8LedDisTmr = FALSE;
				led_para.u8LedDisStep = DIS_RUNNING;
			}
				
			break;
		case DIS_RUNNING:
			if(sys_led_para.bHaveUsb)
			{
				if(RD_CHARGE_STD == FALSE)	//电池电量充满低电平
				{
					LED_CHAR_FULL_ON;
					LED_LOW_POW_OFF;
				}
				else
				{
					LED_CHAR_FULL_OFF;
					LED_LOW_POW_ON;
				}
			}
			else{
				LED_CHAR_FULL_OFF;
				LED_LOW_POW_OFF;
			}
			
			if(led_para.bSubVisitLedSta)	
				LED_SUB_VIS_ON;
			else
				LED_SUB_VIS_OFF;
			
			if(led_para.fRefreshLed)
			{
				led_para.fLedOnOff = ON;
				led_para.u8LedDisTmr = FALSE;
				led_para.fRefreshLed = FALSE;
				fldrv_ertc_alarm_value_set(ALARM_TIME);
				if(led_para.bNetLedSta)								LED_NET_OK_ON;		//网络灯
				else																	LED_NET_BAD_ON;
				if(led_para.bMedLedSta || led_para.u8NetConnSuc)								LED_MED_ALA_ON;		//服药提醒灯	
				else																														LED_MED_ALA_OFF;														
				if(sys_led_para.u8BatEle <= BAT_LOW)
					LED_LOW_POW_ON;
			}
			else
			{
				if(led_para.bNetLedSta != 2)		//没有ID灯常亮
				{
					LED_NET_OK_OFF;
					LED_NET_BAD_OFF;
				}
				
				if(led_para.u8NetConnSuc == FALSE)
				{
					if(led_para.bMedLedSta)
					{
						if(led_para.u8LedDisTmr > 5)	//服药灯闪烁500ms
						{												
							LED_MED_ALA_OFF;
							led_para.fLedOnOff = OFF;
						}
					}
					else{
						LED_MED_ALA_OFF;
						led_para.fLedOnOff = OFF;
					}	
				}
				
				
				if(sys_led_para.bHaveUsb == FALSE)
					LED_LOW_POW_OFF;
			}
			break;
	}
}

void flapp_set_led_refresh(void)
{
	led_para.fRefreshLed = TRUE;
	led_para.u8AlarmCheck = FALSE;
}

u8 flapp_get_led_run_step(void)
{
	return led_para.u8LedDisStep;
}

u8 flapp_get_led_dis_sta(void)
{
	return led_para.fLedOnOff;
}


void flapp_set_led_sta(u8 led,u8 sta)
{
	switch(led)
	{
		case DIS_MED:
			if(0xff ==  sta){
				led_para.fRefreshLed = TRUE;
				led_para.u8NetConnSuc = 50;
			}
			else
				led_para.bMedLedSta = sta;
			break;
		case DIS_SUB:led_para.bSubVisitLedSta = sta;break;
		case DIS_NET:led_para.bNetLedSta = sta;break;
	}
}

